package com.pro01.backend.controller;

import com.pro01.backend.dto.PostSummaryDTO;
import com.pro01.backend.pojo.PageResult;
import com.pro01.backend.pojo.Result;
import com.pro01.backend.service.Forum2PostService;
import com.pro01.backend.utils.TokenUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/forum2")
public class AdminForum2PostController {

    @Autowired
    private Forum2PostService postService;

    // 查看所有帖子列表
    @GetMapping("/posts")
    public Result<PageResult<PostSummaryDTO>> getAllPosts(@RequestParam(defaultValue = "1") int pageNum,
                                                          @RequestParam(defaultValue = "10") int pageSize,
                                                          HttpServletRequest request) {
        String role = TokenUtil.getRole(request);
        if (!"ADMIN".equals(role)) {
            return Result.error("无权限操作，只有管理员可用！");
        }

        PageResult<PostSummaryDTO> result = postService.getAllPostsPaged(pageNum, pageSize);
        return Result.success(result);
    }

    // 删除帖子
    @DeleteMapping("/posts/{postId}")
    public Result<Void> deletePostAsAdmin(@PathVariable Long postId,
                                          HttpServletRequest request) {
        String role = TokenUtil.getRole(request);
        if (!"ADMIN".equals(role)) {
            return Result.error("无权限操作，仅管理员可用");
        }

        int result = postService.deletePostAsAdmin(postId);
        return result > 0 ? Result.success() : Result.error("删除失败或帖子不存在");
    }

    // 隐藏帖子
    @PutMapping("/posts/{postId}/hide")
    public Result<Void> hidePostAsAdmin(@PathVariable Long postId,
                                        HttpServletRequest request) {
        if (!"ADMIN".equals(TokenUtil.getRole(request))) {
            return Result.error("无权限操作，仅管理员可用");
        }

        int result = postService.setPostStatusAsAdmin(postId, "HIDDEN");
        return result > 0 ? Result.success() : Result.error("隐藏失败或帖子不存在");
    }

    // 恢复帖子
    @PutMapping("/posts/{postId}/unhide")
    public Result<Void> unhidePostAsAdmin(@PathVariable Long postId,
                                          HttpServletRequest request) {
        if (!"ADMIN".equals(TokenUtil.getRole(request))) {
            return Result.error("无权限操作，仅管理员可用");
        }

        int result = postService.setPostStatusAsAdmin(postId, "ACTIVE");
        return result > 0 ? Result.success() : Result.error("显示失败或帖子不存在");
    }

    // 置顶帖子
    @PutMapping("/posts/{postId}/pin")
    public Result<Void> pinPost(@PathVariable Long postId,
                                HttpServletRequest request) {
        if (!"ADMIN".equals(TokenUtil.getRole(request))) {
            return Result.error("无权限操作，仅管理员可用");
        }

        int result = postService.setPostPinnedStatus(postId, true);
        return result > 0 ? Result.success() : Result.error("置顶失败或帖子不存在");
    }

    // 取消置顶帖子
    @PutMapping("/posts/{postId}/unpin")
    public Result<Void> unpinPost(@PathVariable Long postId,
                                  HttpServletRequest request) {
        if (!"ADMIN".equals(TokenUtil.getRole(request))) {
            return Result.error("无权限操作，仅管理员可用");
        }

        int result = postService.setPostPinnedStatus(postId, false);
        return result > 0 ? Result.success() : Result.error("取消置顶失败或帖子不存在");
    }

}
